<template>
	<view class="index">
		<view class="customer-wrapper">
			<view class="customer-info">
				<view class="item">
					客户名称：{{ form.customerPetName }}
				</view>
				<view class="item">
					联系电话：{{ form.mobilePhone }}
				</view>
				<view class="item">
					<text v-if="form.status == 3">交付时间：{{ form.deliverTime.substring(0,16) }}</text>
					<text v-else-if="form.status == 2">送达时间：{{ form.mayDeliverDate.substring(0,10) }}</text>
					<text v-else>计划送达：{{ form.mayDeliverDate.substring(0,10) }}</text>
				</view>
			</view>
			<view class="storage-number">
				<view class="code">
					客户号：{{ form.sortCode || '--' }}
				</view>
				<view class="date">
					{{ form.supplyDate.substring(5,10) }} {{ form.schemaCaption }}
				</view>
			</view>
		</view>
		<view class="product-item" v-for="(item,index) in form.orderItems" :key="index">
			<view class="product-name">
				<view class="line"></view>
				<view class="name">
					{{ item.productName }}
				</view>
			</view>
			<view class="product-info">
				<view class="item">
					毛重：{{ item.grossWeight }}斤
				</view>
				<view class="item">
					净重：{{ item.netWeight }}斤
				</view>
				<view class="item">
					货号：{{ item.productCode }}
				</view>
				<view class="item">
					订货量：{{ item.quantity }} 件
				</view>
			</view>
			<view class="product-number">
				<view class="label">
					实配数量
				</view>
				<input type="number" v-model="item.actualQuantity" placeholder="输入实配数量" class="input" />
				<view class="unit">
					件
				</view>
			</view>
			<!-- <view class="tip" v-if="item.actualQuantity && item.actualQuantity < item.quantity">
				<uni-icons type="info-filled" color="#F26022" size="20"></uni-icons>
				<text>缺货{{ item.quantity - item.actualQuantity }}件：提货后由城市仓发起售后为客户退款</text>
			</view> -->
		</view>
		<view class="btn-wrapper">
			<view class="btn default" @click="submit(0)">
				部分提货
			</view>
			<view class="btn" @click="submit(1)">
				全部自提
			</view>
		</view>

		<uni-popup ref="popup">
			<view class="popup-wrapper">
				<view class="title-wrapper">
					确认提货
				</view>
				<view class="form-item">
					<view class="label">
						提货码：
					</view>
					<view class="input-wrapper">
						<input type="digit" auto-focus v-model="code" placeholder="请输入提货码" class="input">
					</view>
				</view>
				<view class="popup-btn-wrapper">
					<view class="btn cancel" @click="confirm(0)">
						熟人,免码提货
					</view>
					<view class="btn" @click="confirm(1)">
						验码提货
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {},
				code: "",
				type: ''
			}
		},
		onLoad(option) {
			this.getInfo(option.id)
		},
		computed: {

		},
		methods: {
			async getInfo(id) {
				const data = await this.$api.get('/app/deliver/order/getInfo', {
					id: id
				})
				// data.data.orderItems.forEach(item => {
				// 	item.actualQuantity =;
				// })
				console.log(data.data)
				this.form = data.data;
			},
			goback() {
				uni.navigateBack()
			},
			close() {
				this.$refs.popup.close()
			},
			submit(type) {
				this.type = type;
				if (type === 1) {
					let msg = "";
					const array = this.form.orderItems;
					array.forEach(item => {
						if (!msg && !item.actualQuantity && item.actualQuantity != '0') {
							msg = `${item.productName}的实配数量不能为空`;
						} else if (!msg && parseFloat(item.actualQuantity) > parseFloat(item.quantity)) {
							msg = `${item.productName}的实配数量不能大于订货数量`;
						}
					});
					if (msg) {
						uni.showToast({
							title: msg,
							icon: 'none',
							duration: 2000
						})
						return
					}
					this.code = '';
					this.$refs.popup.open('center')
				} else {
					let msg = "";
					const array = this.form.orderItems;
					let obj = array.filter(item => {
						if (!msg && parseFloat(item.actualQuantity) > parseFloat(item.quantity)) {
							msg = `${item.productName}的实配数量不能大于订货数量`;
						}
						return item.actualQuantity > 0
					});
					if (!obj.length) {
						msg = "至少要对一个商品进行提货"
					}
					if (msg) {
						uni.showToast({
							title: msg,
							icon: 'none',
							duration: 2000
						})
						return
					}
					this.code = '';
					this.$refs.popup.open('center')
				}

			},
			async confirm(type) {
				if (type === 1 && !this.code) {
					uni.showToast({
						title: '请输入提货码',
						icon: 'none'
					})
					return
				}
				if (type === 1 && this.code !== this.form.captcha) {
					uni.showToast({
						title: '提货码不正确~',
						icon: 'none'
					})
					return
				}
				let params = {
					id: this.form.id,
					deliveryType: this.type === 0 ? 'PART' : 'ALL',
				}
				let array = this.form.orderItems.map(item => {
					return {
						id: item.id,
						actualQuantity: item.actualQuantity,
						quantity: item.quantity
					}
				})
				if (this.type === 0) {
					params.deliveryItems = array.filter(item => {
						return item.actualQuantity > 0
					})
				} else {
					params.deliveryItems = array
				}
				const data = await this.$api.post('/app/deliver/order/delivery', params)
				if (data.code === 200) {
					var pages = getCurrentPages();
					uni.showToast({
						title: '自提成功'
					})
					this.close()
					setTimeout(() => {
						pages[pages.length - 2].$vm.search()
						uni.navigateBack()
					}, 1000)
				}
			}
		},
		components: {

		}
	}
</script>

<style lang="scss" scoped>
	.index {
		// height: 100%;
		// overflow: auto;
		padding-bottom: 150upx;

		.customer-wrapper {
			background-color: #F26022;
			display: flex;
			align-items: center;
			color: #ffffff;
			padding: 30upx;
			border-radius: 0 0 20upx 20upx;
			font-size: 32rpx;

			.customer-info {
				flex: 1;
				border-right: 2upx solid #ffffff;

				.item {
					margin-bottom: 10upx;

					&:last-child {
						margin-bottom: 0upx;
					}
				}
			}

			.storage-number {
				margin-left: 50upx;
				text-align: center;
				font-weight: bold;
				font-size: 38upx;

				.date {
					font-weight: 300;
					font-size: 24rpx;
					color: #ffffff;
					margin-top: 10rpx;
				}
			}
		}

		.product-item {
			background-color: #ffffff;
			border-radius: 20upx;
			margin: 20upx 20upx 0 20upx;
			overflow: hidden;
			padding: 20upx;

			.product-name {
				display: flex;
				align-items: center;

				.line {
					min-width: 6upx;
					height: 30upx;
					background-color: #F26022;
					margin-right: 10upx;
				}

				.name {
					color: #212121;
					font-size: 32upx;
					//width: 500rpx;
					text-overflow: -o-ellipsis-lastline;
					overflow: hidden; //溢出内容隐藏
					text-overflow: ellipsis; //文本溢出部分用省略号表示
					display: -webkit-box; //特别显示模式
					-webkit-line-clamp: 1; //行数
					line-clamp: 1;
					-webkit-box-orient: vertical; //盒子中内容竖直排列
					font-weight: bold;
				}
			}

			.product-info {
				display: flex;
				align-items: center;
				flex-wrap: wrap;

				.item {
					width: 50%;
					font-size: 28upx;
					color: #666666;
					margin-top: 14upx;
				}
			}

			.product-number {
				display: flex;
				align-items: center;
				margin: 20upx 0 0 0;
				border-top: 1upx dashed #efefef;
				padding-top: 20upx;
				font-size: 32upx;
				font-weight: bold;

				.input {
					flex: 1;
					text-align: center;
				}
			}

			.tip {
				display: flex;
				align-items: center;
				font-size: 28upx;
				color: #F26022;
				margin-top: 10upx;
			}
		}

		.btn-wrapper {
			position: fixed;
			height: 100upx;
			width: 100%;
			bottom: 0;
			background-color: #ffffff;
			display: flex;
			align-items: center;
			z-index: 9;

			.btn {
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
				height: 100%;
				background-color: #F26022;
				color: #ffffff;

				&.default {
					color: #232323;
					background-color: #ffffff;
				}
			}
		}

		.popup-wrapper {
			width: 600upx;
			background-color: #ffffff;
			border-radius: 20upx;
			min-height: 300upx;
			overflow: hidden;

			.title-wrapper {
				height: 100upx;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0 32upx;
				font-size: 36upx;
				color: #2B2B2B;
			}

			.form-item {
				margin: 0 44upx;
				height: 100upx;
				display: flex;
				align-items: center;
				border-bottom: 2upx solid #f9f9f9;

				.label {
					font-size: 32upx;
					color: #5B5B5B;
				}

				.input-wrapper {
					height: 70upx;
					flex: 1;
					border: 1upx solid #dedede;
					border-radius: 10upx;
					margin-right: 15upx;

					.input {
						height: 70upx;
						width: 100%;
						text-align: center;
					}
				}
			}

			.popup-btn-wrapper {
				height: 150upx;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0 40upx;

				.btn {
					width: 260upx;
					height: 76upx;
					border-radius: 40upx;
					background-color: #F26022;
					color: #ffffff;
					display: flex;
					align-items: center;
					justify-content: center;
					transition: .15s linear;
					font-size: 32rpx;

					&:active {
						background-color: #F2602266;
					}

					&.cancel {
						box-sizing: border-box;
						background-color: #ffffff;
						border: 2upx solid #F26022;
						margin-right: 40upx;
						color: #F26022;

						&:active {
							background-color: #F2F4FA77;
						}
					}
				}
			}
		}
	}
</style>